#include "CEnter.h"
#include "ui_CEnter.h"
#include <QApplication>
#include "CRunServer.h"
#include "Widget.h"
#include<QCloseEvent>
#include <QMenuBar>
CEnter::CEnter(QWidget *parent)
    :QWidget(parent)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    setIcon(":/logo.png");
    setTitle(tr("Chat"));
    _trayIcon=new QSystemTrayIcon(this);
    server=0;
    client=0;
    initMenu();
    connect(_trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(onActiveTray(QSystemTrayIcon::ActivationReason)));
    connect(ui->labelMin,SIGNAL(clicked(bool)),this,SLOT(close()));
    _trayIcon->setIcon(QIcon(":/logo.png"));
    _trayIcon->setToolTip(tr("Chat"));
    _trayIcon->show();
}

void CEnter::onActiveTray(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::Context:
        onShowMenu();
        break;
    case QSystemTrayIcon::DoubleClick:
        onShowMessage();
        break;
    case QSystemTrayIcon::Trigger:
        onShowWindow();
        break;

    default:
        break;
    }
}

void CEnter::onShowMenu(){
}

void CEnter::onShowWindow()
{
    this->show();
}

void CEnter::onShowMessage(){}

void CEnter::onClose()
{
    exit(0);
}

void CEnter::onRunserver()
{
    if(!server)
    {
        server=new CRunServer(this);
        server->setWindowFlag(Qt::Dialog);
    }
    server->show();
}

void CEnter::onLogin()
{
    if(!client)
    {
        client=new Widget(this);
        client->setWindowFlag(Qt::Dialog);
    }
    client->show();
}

void CEnter::setIcon(const QString &icon)
{
    QWidget::setWindowIcon(QIcon(icon));
}

void CEnter::setTitle(const QString &title)
{
    ui->labelTitle->setText(title);
    setWindowTitle(title);
}





void CEnter::closeEvent(QCloseEvent *event)
{
    event->ignore();
    this->hide();
}

void CEnter::initMenu()
{
    QAction*showMessage=new QAction(tr("show message"));
    QAction*close=new QAction(tr("close"));
    QAction*runServer=new QAction(tr("run as server"));
    QAction*login=new QAction(tr("login"));
    connect(showMessage,SIGNAL(triggered(bool)),this,SLOT(onShowMessage()));
    connect(close,SIGNAL(triggered(bool)),this,SLOT(onClose()));
    connect(runServer,SIGNAL(triggered(bool)),this,SLOT(onRunserver()));
    connect(login,SIGNAL(triggered(bool)),this,SLOT(onLogin()));
    QList<QAction*> items;
    items.append(login);
    items.append(showMessage);
    items.append(runServer);
    items.append(close);

    QMenu* m1=new QMenu(this);
    m1->addActions(items);
    _trayIcon->setContextMenu(m1);
    QMenu*m2=new QMenu(this);
    m2->addActions(items);
    ui->pushButtonSetting->setMenu(m2);
    ui->pushButtonSetting->setStyleSheet("QPushButton{border-image:url(:/menu.png)}QPushButton::menu-indicator{image:none}");
}

void CEnter::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons()&Qt::LeftButton)
    {
        QPoint pos=e->globalPos()-pressPosition;
        move(pos);
    }
}

void CEnter::mousePressEvent(QMouseEvent *e)
{
    if(e->buttons()&Qt::LeftButton)
    {
        pressPosition=e->globalPos()-this->frameGeometry().topLeft();
    }
}























